//
//  HWToolView.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/14.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWToolView.h"
#import "HWStatus.h"
@interface HWToolView ()
/**里面存放所有的按钮*/
@property (nonatomic, strong) NSMutableArray *btns;
/**里面存放所有分割线*/
@property (nonatomic, strong) NSMutableArray *dividers;
@property (nonatomic, weak) UIButton *repostBtn;
@property (nonatomic, weak) UIButton *commentBtn;
@property (nonatomic, weak) UIButton *attitudeBtn;
@end
@implementation HWToolView
#pragma mark - 懒加载
- (NSMutableArray *)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (NSMutableArray *)dividers{
    if (!_dividers) {
        _dividers = [NSMutableArray array];
    }
    return _dividers;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"timeline_card_bottom_background"]];
        self.attitudeBtn = [self setupBtnWithImageName:@"timeline_icon_unlike" title:@"赞"];
        self.backgroundColor = [UIColor whiteColor];
       self.repostBtn = [self setupBtnWithImageName:@"timeline_icon_retweet" title:@"转发"];
      self.commentBtn = [self setupBtnWithImageName:@"timeline_icon_comment" title:@"评论"];
//       设置分割线
        [self setupDivider];
        [self setupDivider];
    }
    return self;
}


- (void)setStatus:(HWStatus *)status{
    _status = status;
    //转发
    [self setupBtnCount:status.reposts_count button:self.repostBtn];
    
    //评论
    [self setupBtnCount:status.comments_count button:self.commentBtn];
    //赞
    [self setupBtnCount:status.attitudes_count button:self.attitudeBtn];
}

- (void)setupBtnCount:(int)count button:(UIButton *)btn{
    if (count) {
        if (count > 10000) {
            double wan = count / 10000.0;
            NSString *title = [NSString stringWithFormat:@"%.1f万", wan];
          title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
            [btn setTitle:title forState:UIControlStateNormal];
        }else{
            [btn setTitle:[NSString stringWithFormat:@"%d",count] forState:UIControlStateNormal];
        }
    }
}
/**
 * 添加分割线
 */
- (void)setupDivider
{
    UIImageView *divider = [[UIImageView alloc] init];
    divider.image = [UIImage imageNamed:@"timeline_card_bottom_line"];
    [self addSubview:divider];
    [self.dividers addObject:divider];
}

- (UIButton *)setupBtnWithImageName:(NSString *)imageName title:(NSString *)title{
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setBackgroundImage:[UIImage imageNamed:@"timeline_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    NSInteger count = self.btns.count;
    CGFloat width = self.width / count;
    CGFloat height = self.height;
    CGFloat y = 0;
    
    for (int i = 0; i<count; i++) {
        UIButton *btn = self.subviews[i];
        btn.x = i * width;
        btn.y = y;
        btn.width = width;
        btn.height = height;
    }
    // 设置分割线的frame
    NSInteger dividerCount = self.dividers.count;
    for (int i = 0; i<dividerCount; i++) {
        UIImageView *divider = self.dividers[i];
        divider.width = 1;
        divider.height = height;
        divider.x = (i + 1) * width;
        divider.y = 0;
    }
}
@end
